package com.android.aircondition.view;


import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.aircondition.R;
import com.android.aircondition.constant.AppConstant;
import com.android.aircondition.service.GreeService;
import com.android.aircondition.service.Update;

public class SubTabUpdateView extends LinearLayout implements OnClickListener{
	
	private LinearLayout firstLinear;
	private LinearLayout secondLinear;
	private LinearLayout thirdLinear;
	private LinearLayout fourthLinear;
	private LinearLayout fifthLinear;
	
	private ImageView   firstImv;
	private ImageView   secondImv;
	private ImageView   thirdImv;
	private ImageView   fourthImv;
	private ImageView   fifthImv;
	
	private TextView    firstText;
	private TextView    secondText;
	private TextView    thirdText;
	private TextView    fourthText;
	private TextView    fifthText;	
	
	private Boolean key;
	
	private GreeService service;
	
	public SubTabUpdateView(Context context) {
		super(context,null);
	}

	public SubTabUpdateView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		LinearLayout linear = (LinearLayout)LayoutInflater.from(context).inflate(R.layout.sub_direction, this, true);
		  
		firstLinear  = (LinearLayout)linear.findViewById(R.id.Direction_subtab_first);
		secondLinear = (LinearLayout)linear.findViewById(R.id.Direction_subtab_second);
		thirdLinear  = (LinearLayout)linear.findViewById(R.id.Direction_subtab_third);
		fourthLinear = (LinearLayout)linear.findViewById(R.id.Direction_subtab_fourth);
		fifthLinear  = (LinearLayout)linear.findViewById(R.id.Direction_subtab_fifth);
		
		firstImv     = (ImageView   )linear.findViewById(R.id.sub_imv_first);
		secondImv    = (ImageView   )linear.findViewById(R.id.sub_imv_second);
		thirdImv     = (ImageView   )linear.findViewById(R.id.sub_imv_third);
		fourthImv    = (ImageView   )linear.findViewById(R.id.sub_imv_fourth);
		fifthImv     = (ImageView   )linear.findViewById(R.id.sub_imv_fifth);
		
		firstText    = (TextView    )linear.findViewById(R.id.sub_tv_first);
		secondText   = (TextView    )linear.findViewById(R.id.sub_tv_second);
		thirdText    = (TextView    )linear.findViewById(R.id.sub_tv_third);
		fourthText   = (TextView    )linear.findViewById(R.id.sub_tv_fourth);
		fifthText    = (TextView    )linear.findViewById(R.id.sub_tv_fifth);
		
		initData(context);
		
	}

	private void initData(Context context){
		
		firstImv .setImageResource(R.drawable.sub_tab_xiqi);
		secondImv.setImageResource(R.drawable.sub_tab_paiqi);
		thirdImv .setImageResource(R.drawable.sub_tab_zhineng_xiqi);
		fourthImv.setImageResource(R.drawable.sub_tab_zhineng_paiqi);
		fifthImv .setImageResource(R.drawable.sub_tab_guanbi);
		
		String[] array = context.getResources().getStringArray(R.array.sub_tab_update);
		firstText .setText(array[AppConstant.NO_0]);
		secondText.setText(array[AppConstant.NO_1]);
		thirdText .setText(array[AppConstant.NO_2]);
		fourthText.setText(array[AppConstant.NO_3]);
		fifthText .setText(array[AppConstant.NO_4]);
		
		firstLinear .setOnClickListener(this);
		secondLinear.setOnClickListener(this);
		thirdLinear .setOnClickListener(this);
		fourthLinear.setOnClickListener(this);
		fifthLinear .setOnClickListener(this);
		
		service = new GreeService(context);
		Update update = service.getUpdatePreference();
		
		int alphaXiqi         = update.getXiqi()?255:150;
		int alphaPaiqi        = update.getPaiqi()?255:150;
		int alphaZhinengxiqi  = update.getZhinengxiqi()?255:150;
		int alphaZhinengpaiqi = update.getZhinengpaiqi()?255:150;
		int alphaGuanbi       = update.getGuanbi()?255:150;
		
		firstImv.setAlpha(alphaXiqi);
		secondImv.setAlpha(alphaPaiqi);
		thirdImv.setAlpha(alphaZhinengxiqi);
		fourthImv.setAlpha(alphaZhinengpaiqi);
		fifthImv.setAlpha(alphaGuanbi);
		
		key = false;
	}
	
	public void onClick(View v) {
		
		if(key) return;
		
		key = true;
		
		Update update = service.getUpdatePreference();
		
		switch (v.getId()) {
		
		case R.id.Direction_subtab_first:
			
			if(update.getXiqi()){
				firstImv.setAlpha(150);
				update.setXiqi(false);
			}else{
				firstImv.setAlpha(255);
				update.setXiqi(true);	
			}
			
			break;
			
		case R.id.Direction_subtab_second:
			
			if(update.getPaiqi()){
				secondImv.setAlpha(150);
				update.setPaiqi(false);
			}else{
				secondImv.setAlpha(255);
				update.setPaiqi(true);	
			}
			
			break;
			
		case R.id.Direction_subtab_third:
			
			if(update.getZhinengxiqi()){
				
				thirdImv.setAlpha(150);
				update.setZhinengxiqi(false);
				
			}else{
				thirdImv.setAlpha(255);
				update.setZhinengxiqi(true);	
			}
			
			break;
			
		case R.id.Direction_subtab_fourth:
			
			if(update.getZhinengpaiqi()){
				fourthImv.setAlpha(150);
				update.setZhinengpaiqi(false);
			}else{
				fourthImv.setAlpha(255);
				update.setZhinengpaiqi(true);	
			}
			
			break;
			
		case R.id.Direction_subtab_fifth:
			
			if(update.getGuanbi()){
				fifthImv.setAlpha(150);
				update.setGuanbi(false);
			}else{
				fifthImv.setAlpha(255);
				update.setGuanbi(true);	
			}
			
			break;

		}
		
		service.setUpdatePreference(update);
		
		key = false;
	}
	
	
	
}
